<?php

namespace App\Module\Api\Controllers;
use App\Module\Api\Controllers\Request\UserUpdatePayPasswordRequest;
use App\Module\Api\Validator\UserUpdatePayPasswordValidator;
use App\System\Base\Repositories\BaseSmsCodeRepository;
use App\System\Base\Services\TokenService;
use App\System\Cis\Repositories\CisUserRepository;

/**
 * 修改支付密码
 */
class UserUpdatePayPasswordController extends Controller
{

    public function __construct()
    {
        $this->query = new UserUpdatePayPasswordRequest();
        parent::__construct();

    }

    public function index()
    {
        (new UserUpdatePayPasswordValidator())->check();
        $user_id = TokenService::getMasterId();
        $smsCode = BaseSmsCodeRepository::getInstance();
        $check_code = $smsCode->getOneById($this->query->smsKey,7);
        if($check_code['s']) {
            return apiResponse($check_code);
        }
        $user = CisUserRepository::getInstance();
        $user_info = $user->getUser($user_id);
        if($user_info->pay_password == $this->query->payPassword)
        {
            return apiResponse(['s'=>'10000','d'=>'新密码不能与旧密码相同']);
        }
        $res = $user->updateUser(['pay_password'=>$this->query->payPassword],$user_id);
        if(!$res) return apiResponse(['s'=>'1000','d'=>'密码更新失败']);
        return apiResponse();
    }
}
